<?php
class Club_Model_Event extends Zend_Db_Table{
	
	protected $_name = 'event';
	protected $_primary ='id';
	
	protected $_ids;
	
	public function getItem($arrParam = null, $options = null){
		
		if($options['task'] == 'admin-info' || $options['task'] == 'admin-edit'){
			$db = Zend_Registry::get('connectDb');
			//$db = Zend_Db::factory($adapter, $config);
			$select = $db->select()
			->from('event AS c',array("*","DATE_FORMAT(c.date,'%d/%m/%Y') AS date","DATE_FORMAT(c.created,'%d/%m/%Y') AS created"))
			->joinLeft('clb AS cc', 'c.id_clb = cc.id',array('cc.name AS name_clb'))
 			->joinLeft('users AS u', 'c.createby = u.id',array('u.user_name AS name_createby'))
 			->where('c.typer = 1')
			->where('c.id = ?', $arrParam['id'], INTEGER);
			//echo $select;	
			$result = $db->fetchRow($select);
		}
		 
		return $result;
	}
	
	public function countItem($arrParam = null, $options = null){
		$ssFilter  = $arrParam['ssFilter'];
		if($options == null){
			$db = Zend_Registry::get('connectDb');
			//$db = Zend_Db::factory($adapter, $config);
			$select = $db->select()
			->from('event AS c', array('COUNT(c.id) AS totalItem'))
			->where('c.typer = ?', '1');
			$result = $db->fetchOne($select);
		}
		if($options['task'] == 'admin-list'){
			$db = Zend_Registry::get('connectDb');
			//$db = Zend_Db::factory($adapter, $config);
			
			$select = $db->select()
			->from('event AS c', array('COUNT(c.id) AS totalItem'))
			->where('c.typer = ?', '1');
			if(!empty($ssFilter['keywords'])){
				$keywords = '%' . $ssFilter['keywords'] . '%';
				$select->where('c.name LIKE ?', $keywords, STRING);
			}
			
			if(!empty($ssFilter['blocks'])){
				$select->where('c.' . $ssFilter['blocks'] . '= ?', 1, INTERGER);
			}
			
			if(!empty($ssFilter['id_clb'])){
				$select->where('c.id_clb = ?', $ssFilter['id_clb']);
			}
			
			if(!empty($ssFilter['status'])){
				$select->where('c.status = ?', $ssFilter['status']);
			}
			
			$result = $db->fetchOne($select);
		}
		
		return $result;
	}
	
	public function itemInSelectbox($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
		$ssFilter  = $arrParam['ssFilter'];
		if($options == null){
			$select = $db->select()
			->from('clb AS cc',array('id','name','status'))
			->order('cc.id ASC');
			$result = $db->fetchPairs($select);
			
		}
		//var_dump($result);die;
		return $result;
	}
	
	public function userInSelectbox($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
		$ssFilter  = $arrParam['ssFilter'];
		if($options == null){
			$select = $db->select()
			->from('users AS cc',array('id','name','status'))
			->order('cc.id ASC');
			$result = $db->fetchPairs($select);
				
		}
		//var_dump($result);die;
		return $result;
	}
	
	public function listItem($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
		$ssFilter  = $arrParam['ssFilter'];
		if($options['task'] == 'admin-list'){
			
			$paginator = $arrParam['paginator'];
			
			
			$select = $db	->	select()
							->from('event AS c',array("*","DATE_FORMAT(c.date,'%d/%m/%Y') AS date","DATE_FORMAT(c.created,'%d/%m/%Y') AS created"))
							->joinLeft('clb AS cc', 'c.id_clb = cc.id',array('cc.name AS name_clb'))
				 			->joinLeft('users AS u', 'c.createby = u.id',array('u.user_name AS name_createby'))
							->where('c.typer = 1');
							//->where('c.id = ?', $arrParam['id'], INTEGER);
				
			//echo $select;die;
			if($paginator['itemCountPerPage'] > 0){
				$page = $paginator['currentPage'];
				$rowCount = $paginator['itemCountPerPage'];
				$select->limitPage($page, $rowCount);
			}
			
			if(!empty($ssFilter['id_clb'])){
				$select->where('c.id_clb = ?', $ssFilter['id_clb']);
			}
			
			if(!empty($ssFilter['status'])){
				$select->where('c.status = ?', $ssFilter['status']);
			}
			$result = $db->fetchAll($select);
		}
		
		return $result;
		
	}

	public function saveItem($arrParam = null, $options = null){
		//var_dump($arrParam);die;
		if($options['task'] == 'admin-add'){
			$row 				= $this->fetchNew();
			$info 					= new Zendvn_System_Info();
			$createby 			= $info->getMemberInfo('id');

			$row->title 			= stripslashes($arrParam['title']);
			$row->id_clb 			= stripslashes($arrParam['id_clb']);
			$row->content			= stripslashes($arrParam['content']);
			
			$date     = new Zend_Date();
			$date->set($arrParam['date']);
			$date    = $date->toString('YYYY-MM-dd');
			$row->date 			= $date;
			
			$row->created 			= @date("Y-m-d h:i:s");
			$row->createby			= $createby;
			$row->status 			= $arrParam['status'];
			$row->lang_code 		= $arrParam['lang_code'];
			$row->typer		 		= '1';
			
			$row->save();
		}
	
		if($options['task'] == 'admin-edit'){
				
			$where 				= ' id=' . $arrParam['id'];
			$row 				= $this->fetchRow($where);
		
			$info 					= new Zendvn_System_Info();
			$createby 			= $info->getMemberInfo('id');

			$row->title 			= stripslashes($arrParam['title']);
			$row->id_clb 			= stripslashes($arrParam['id_clb']);
			$row->content			= stripslashes($arrParam['content']);
			
			$date     = new Zend_Date();
			$date->set($arrParam['date']);
			$date    = $date->toString('YYYY-MM-dd');
			$row->date 			= $date;
			
			$row->created 			= @date("Y-m-d h:i:s");
			$row->createby			= $createby;
			$row->status 			= $arrParam['status'];
			$row->lang_code 		= $arrParam['lang_code'];
			$row->typer		 		= '1';
			
			$row->save();
		}
		
		if($options['task'] == 'public-hits'){
			$where 				= ' id=' . $arrParam['id'];
			$row 				= $this->fetchRow($where);
			
			$row->hits 		= $arrParam['hits'] + 1;
			
			$row->save();
		}
	}

	public function deleteItem($arrParam = null, $options = null){
		if($options['task'] == 'admin-delete'){				
			$where = ' id=' . $arrParam['id'];
			$result = $this->delete($where);
		}
	
		if($options['task'] == 'admin-delete-muti'){
			$cid = explode(',', $arrParam['cid']);
			if(!empty($cid) && isset($arrParam['cid'])){
				$ids = implode(',', $cid);
				$where = 'id IN (' . $ids . ')';
				$this->delete($where);
			}
	
		}
	}
	
	public function changeStatus($arrParam = null, $options = null){
		$cid = $arrParam['cid'];
		if(count($cid) > 0){
			if($arrParam['type'] == 1){
				$status = 1;
			}else{
				$status = 0;
			}
	
			$id = implode(',', $cid);
			$data = array('status' => $status);
			$where = 'id IN (' . $id . ')';
			$this->update($data, $where);
		}
		if($arrParam['id'] > 0){
			if($arrParam['type'] == 1){
				$status = 1;
			}else{
				$status = 0;
			}
			$data = array('status' => $status);
			$where = 'id = ' . $arrParam['id'];
			$this->update($data, $where);
		}
	}
	
	public function sortItem($arrParam = null, $options = null){
		$cid = $arrParam['cid'];
		$order = $arrParam['order'];
		if($options['task'] == 'admin-sort'){
			if(count($cid) > 0){
				foreach ($cid as $key => $val){
					$data = array('order' => $order[$val]);
					$where = 'id = ' . $val;
					$this->update($data, $where);
				}
			}
		}
	}
}